<?php

namespace app\common\cachemodel\chakra;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class ChakraCrystalGoods extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.chakra_crystal_goods';

    protected $cacheModel = 'chakra.crystal_goods';

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->whereIn('id', $ids)->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $index => $itemData)
        {
            $resData[$index] = $this->dataInit($itemData);
        }

        return $resData;
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        $data = arrayKeyData2int($data, ['id', 'status', 'sort', 'aid', 'create_time', 'update_time']);

        if ($data['tids'])
        {
            $tids = $data['tids'] ? explode(',', $data['tids']) : [];

            $data['tids'] = array_map('intval', $tids);
        }

        return $data;
    }

    public function getListIds()
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc')
            {
                try
                {
                    $data = $this->where(['status' => 1])->order("sort desc,id desc")->column('id');

                    $resData = [];
                    foreach ($data as $id)
                    {
                        $resData[$id] = ['id' => $id];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0);

        return array_keys($resData['data']);
    }
}